import { getBaseUrl } from '@/services/urlconst';
import { getLoadMapList, getSelectDataMapList, getTenantID, requestFunComponFiex } from '@/utils';
import { useCallback, useMemo } from 'react';
import { useRequest } from 'umi';
import { WasteMaterialsAPI } from '../common/type';
import { ManageSearchColumns } from '../mconfig';
import { MaterialAndAssetManageTab } from '../mconfig/ManageTabSwitch';
import { ILoadMapTypeAll } from '@/common';

let baseUrl = getBaseUrl();
// let baseUrl = '/wenchuang'; // 本地环境
const assetListManageServices = (curRes: Record<string, any>) => {
  /***获取数据字典**/
  const loadMdicIds: string[] = [
    `${getTenantID()}${ILoadMapTypeAll.MATERIAL_TYPE}`,
    `${getTenantID()}${ILoadMapTypeAll.MATERIEL_STATUS}`,
    `${getTenantID()}${ILoadMapTypeAll.MATERIAL_BRAND}`,
  ];

  const loadMdicMapObj: Record<string, any> = getLoadMapList(loadMdicIds);

  /**获取权限参数 */
  const queryTypeObj: Record<string, any> = MaterialAndAssetManageTab(curRes);
  /**
   * 获取列表数据
   */
  const { tableProps, data, error, loading, run, params } = useRequest(
    (opt: WasteMaterialsAPI.IAssetTableRequestType) => {
      let personUrl = `${baseUrl}/assetStuff/dataGrid/${queryTypeObj?.params}`;
      let personBody: any = {
        ...queryTypeObj,
        order: 'desc',
        page: 1,
        rows: 10,
        sort: 'createTime',
        dicTypeIds: loadMdicIds?.toString(), // 字典入参
        assetState: `${getTenantID()}${ILoadMapTypeAll.ASSET_STATUS}1007`, // 查询待报废
        ...opt,
      };
      return requestFunComponFiex('POST', personUrl, personBody);
    },
    {
      manual: true, //页面初始请求为true
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /* 获取页面数据 */
  const getAssetListTableData = useCallback((otherRequest: WasteMaterialsAPI.IAssetTableRequestType | any) => {
    const request: WasteMaterialsAPI.IAssetTableRequestType | any = {
      ...otherRequest,
    };

    run({
      ...request,
    });
  }, []);

  /** 获取搜索参数 */
  const searchListParams = ManageSearchColumns(loadMdicMapObj?.loadMapInfo, queryTypeObj);

  /* 页面列表 */
  let tableValues = useMemo(() => {
    return {
      data: data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  /***搜索功能配置columns**/
  const searchList = useMemo(() => {
    return {
      searchList: searchListParams,
    };
  }, [loadMdicMapObj?.loadMapInfo]);

  /* 数据字典值  */
  let loadMapList = useMemo(() => {
    return {
      loadMdicMapList: [],
    };
  }, [params]);

  return {
    loading,
    getAssetListTableData, //其他请求操作方法例如刷新等等
    // manageTableParams,
    otherParams: params?.[0],
    ...tableValues, //页面列表值
    ...loadMapList,
    ...searchList,
  };
};

export { assetListManageServices };
